﻿/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />
/// <reference path="//Microsoft.WinJS.1.0/js/ui.js" />

(function () {
    "use strict";

    var style = "dark";

    WinJS.UI.Pages.define("/default.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();

            dataTransferManager.addEventListener("datarequested", function (e) {
                var request = e.request;

                request.failWithDisplayText("This part of the application does not support sharing.");

                //var range = document.createRange();
                //range.selectNode(dices);
                //request.data = MSApp.createDataPackage(range);
                //request.data.properties.title = "Dice Roller";
                //request.data.properties.description = "Look at what I just rolled!";
            });

            var offset = { top: "0px", left: "-2000px" };
            var el = document.getElementById("menuContent");

            WinJS.UI.Animation.enterPage(el, offset);

            WinJS.Utilities.query("nav ul li a").listen("click", function (e) {
                e.preventDefault();
                //document.getElementById("debug").innerHTML = e.target;
                window.location(e.target);
                //5WinJS.UI.Animation.exitPage(el, offset);
            });
            //WinJS.UI.Animation.exitPage(el, offset);

            // Populate settings pane and tie commands to settings flyouts.
            WinJS.Application.onsettings = function (e) {
                e.detail.applicationcommands = {
                    "privacyDiv": { href: "PrivacyUI.html", title: "Privacy Policy" },
                    "help": { href: "HelpUI.html", title: "Help" }
                };
                WinJS.UI.SettingsFlyout.populateSettings(e);
            };

            WinJS.Application.start();

            document.getElementById("cmdHome").addEventListener("click", doClickHome, false);
            document.getElementById("cmdTextBased").addEventListener("click", doClickTextBased, false);
            document.getElementById("cmdGraphic").addEventListener("click", doClickGraphic, false);
            document.getElementById("cmdChangeUI").addEventListener("click", doClickChangeUI, false);
        },

        unload: function () {
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });
    WinJS.UI.Pages.define("/graphic.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();

            dataTransferManager.addEventListener("datarequested", function (e) {
                var request = e.request;

                request.failWithDisplayText("This part of the application does not support sharing.");

                //var range = document.createRange();
                //range.selectNode(dices);
                //request.data = MSApp.createDataPackage(range);
                //request.data.properties.title = "Dice Roller";
                //request.data.properties.description = "Look at what I just rolled!";
            });

            var offset = { top: "0px", left: "-2000px" };
            var el = document.getElementById("canvasContent");
            WinJS.UI.Animation.enterPage(el, offset);
            WinJS.Utilities.query("button.win-backbutton").listen("click", function (e) {
                e.preventDefault();
                //document.getElementById("debug").innerHTML = e.target;
                window.location('/default.html');
                //5WinJS.UI.Animation.exitPage(el, offset);
            });
            //WinJS.UI.Animation.exitPage(el, offset);

            // Populate settings pane and tie commands to settings flyouts.
            WinJS.Application.onsettings = function (e) {
                e.detail.applicationcommands = {
                    "privacyDiv": { href: "PrivacyUI.html", title: "Privacy Policy" },
                    "helpGraphic": { href: "HelpGraphicUI.html", title: "Help" }
                };
                WinJS.UI.SettingsFlyout.populateSettings(e);
            };

            WinJS.Application.start();

            document.getElementById("cmdHome").addEventListener("click", doClickHome, false);
            document.getElementById("cmdTextBased").addEventListener("click", doClickTextBased, false);
            document.getElementById("cmdGraphic").addEventListener("click", doClickGraphic, false);
            document.getElementById("cmdChangeUI").addEventListener("click", doClickChangeUI, false);
        },

        unload: function () {
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });
    WinJS.UI.Pages.define("/text-based.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();

            dataTransferManager.addEventListener("datarequested", function (e) {
                var request = e.request;

                var range = document.createRange();
                range.selectNode(document.getElementById("dices"));
                request.data = MSApp.createDataPackage(range);
                request.data.properties.title = "Dice Roller";
                request.data.properties.description = "Look at what I just rolled!";
                
                var path = "ms-appx:/images/dice.jpg";
                var imageUri = new Windows.Foundation.Uri(path);
                var streamRefference = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(imageUri);
                request.data.resourceMap[path] = streamRefference;
            });

            var offset = { top: "0px", left: "-2000px" };
            var el = document.getElementById("diceContent");

            WinJS.UI.Animation.enterPage(el, offset);

            WinJS.Utilities.query("button.win-backbutton").listen("click", function (e) {
                e.preventDefault();
                //document.getElementById("debug").innerHTML = e.target;
                window.location('/default.html');
                //5WinJS.UI.Animation.exitPage(el, offset);
            });

            WinJS.Utilities.query("div.button button").listen("click", function (e) {
                e.preventDefault();
                switch (e.target.id) {
                    case "addD4":
                        Dices.d4.increase();
                        noOfD4.value = Dices.d4.getNumber();
                        break;
                    case "delD4":
                        Dices.d4.decrease();
                        noOfD4.value = Dices.d4.getNumber();
                        break;
                    case "addD6":
                        Dices.d6.increase();
                        noOfD6.value = Dices.d6.getNumber();
                        break;
                    case "delD6":
                        Dices.d6.decrease();
                        noOfD6.value = Dices.d6.getNumber();
                        break;
                    case "addD8":
                        Dices.d8.increase();
                        noOfD8.value = Dices.d8.getNumber();
                        break;
                    case "delD8":
                        Dices.d8.decrease();
                        noOfD8.value = Dices.d8.getNumber();
                        break;
                    case "addD10":
                        Dices.d10.increase();
                        noOfD10.value = Dices.d10.getNumber();
                        break;
                    case "delD10":
                        Dices.d10.decrease();
                        noOfD10.value = Dices.d10.getNumber();
                        break;
                    case "addDPercent":
                        Dices.dPercent.increase();
                        noOfDPercent.value = Dices.dPercent.getNumber();
                        break;
                    case "delDPercent":
                        Dices.dPercent.decrease();
                        noOfDPercent.value = Dices.dPercent.getNumber();
                        break;
                    case "addD12":
                        Dices.d12.increase();
                        noOfD12.value = Dices.d12.getNumber();
                        break;
                    case "delD12":
                        Dices.d12.decrease();
                        noOfD12.value = Dices.d12.getNumber();
                        break;
                    case "addD20":
                        Dices.d20.increase();
                        noOfD20.value = Dices.d20.getNumber();
                        break;
                    case "delD20":
                        Dices.d20.decrease();
                        noOfD20.value = Dices.d20.getNumber();
                        break;
                    case "roll":
                        if (noOfD4.value == 0 &&
                            noOfD6.value == 0 &&
                            noOfD8.value == 0 &&
                            noOfD10.value == 0 &&
                            noOfDPercent.value == 0 &&
                            noOfD12.value == 0 &&
                            noOfD20.value == 0) {
                            document.getElementById("error").innerText = "Please, for the love of RPG games, add some dices to roll.";
                        } else {
                            document.getElementById("error").innerText = "";
                            WinJS.Utilities.empty(document.getElementById("dices"));

                            var divD4 = document.createElement("div");
                            divD4.id = "d4ResultsContent";
                            divD4.innerText = "d4: ";
                            for (var i = 1; i <= noOfD4.value ; i++) {
                                var rolValue = Dices.d4.rollDice();
                                if (i != noOfD4.value) {
                                    divD4.innerText = divD4.innerText + rolValue + ', ';
                                } else {
                                    divD4.innerText = divD4.innerText + rolValue;
                                }
                            }

                            var divD6 = document.createElement("div");
                            divD6.id = "d6ResultsContent";
                            divD6.innerText = "d6: ";
                            for (var i = 1; i <= noOfD6.value ; i++) {
                                var rolValue = Dices.d6.rollDice();
                                if (i != noOfD6.value) {
                                    divD6.innerText = divD6.innerText + rolValue + ', ';
                                } else {
                                    divD6.innerText = divD6.innerText + rolValue;
                                }
                            }

                            var divD8 = document.createElement("div");
                            divD8.id = "d8ResultsContent";
                            divD8.innerText = "d8: ";
                            for (var i = 1; i <= noOfD8.value ; i++) {
                                var rolValue = Dices.d8.rollDice();
                                if (i != noOfD8.value) {
                                    divD8.innerText = divD8.innerText + rolValue + ', ';
                                } else {
                                    divD8.innerText = divD8.innerText + rolValue;
                                }
                            }

                            var divD10 = document.createElement("div");
                            divD10.id = "d10ResultsContent";
                            divD10.innerText = "d10: ";
                            for (var i = 1; i <= noOfD10.value ; i++) {
                                var rolValue = Dices.d10.rollDice();
                                if (i != noOfD10.value) {
                                    divD10.innerText = divD10.innerText + rolValue + ', ';
                                } else {
                                    divD10.innerText = divD10.innerText + rolValue;
                                }
                            }

                            var divDPercent = document.createElement("div");
                            divDPercent.id = "dPercentResultsContent";
                            divDPercent.innerText = "dPercent: ";
                            for (var i = 1; i <= noOfDPercent.value ; i++) {
                                var rolValue = Dices.dPercent.rollDice();
                                if (i != noOfDPercent.value) {
                                    divDPercent.innerText = divDPercent.innerText + rolValue + '%, ';
                                } else {
                                    divDPercent.innerText = divDPercent.innerText + rolValue + '%';
                                }
                            }

                            var divD12 = document.createElement("div");
                            divD12.id = "d12ResultsContent";
                            divD12.innerText = "d12: ";
                            for (var i = 1; i <= noOfD12.value ; i++) {
                                var rolValue = Dices.d12.rollDice();
                                if (i != noOfD12.value) {
                                    divD12.innerText = divD12.innerText + rolValue + ', ';
                                } else {
                                    divD12.innerText = divD12.innerText + rolValue;
                                }
                            }

                            var divD20 = document.createElement("div");
                            divD20.id = "d20ResultsContent";
                            divD20.innerText = "d20: ";
                            for (var i = 1; i <= noOfD20.value ; i++) {
                                var rolValue = Dices.d20.rollDice();
                                if (i != noOfD20.value) {
                                    divD20.innerText = divD20.innerText + rolValue + ', ';
                                } else {
                                    divD20.innerText = divD20.innerText + rolValue;
                                }
                            }

                            var dices = document.getElementById("dices");

                            if (noOfD4.value != 0) {
                                dices.appendChild(divD4);
                            }
                            if (noOfD6.value != 0) {
                                dices.appendChild(divD6);
                            }
                            if (noOfD8.value != 0) {
                                dices.appendChild(divD8);
                            }
                            if (noOfD10.value != 0) {
                                dices.appendChild(divD10);
                            }
                            if (noOfDPercent.value != 0) {
                                dices.appendChild(divDPercent);
                            }
                            if (noOfD12.value != 0) {
                                dices.appendChild(divD12);
                            }
                            if (noOfD20.value != 0) {
                                dices.appendChild(divD20);
                            }

                            var diceRollHistory = document.getElementById("diceRollHistory");
                            diceRollHistory.innerHTML = dices.innerHTML + "<br />" + diceRollHistory.innerHTML;
                        }
                        break;
                    default:
                        break;
                }
            });

            WinJS.Utilities.query("body").listen("key down", function (e) {
                if (e.keyCode == WinJS.Utilities.Key.enter) {
                    document.getElementById("roll").click();
                }
            }, false);

            //WinJS.UI.Animation.exitPage(el, offset);

            // Populate settings pane and tie commands to settings flyouts.
            WinJS.Application.onsettings = function (e) {
                e.detail.applicationcommands = {
                    "privacyDiv": { href: "PrivacyUI.html", title: "Privacy Policy" },
                    "helpTextBased": { href: "HelpTextBasedUI.html", title: "Help" }
                };
                WinJS.UI.SettingsFlyout.populateSettings(e);
            };

            WinJS.Application.start();

            document.getElementById("cmdHome").addEventListener("click", doClickHome, false);
            document.getElementById("cmdTextBased").addEventListener("click", doClickTextBased, false);
            document.getElementById("cmdGraphic").addEventListener("click", doClickGraphic, false);
            document.getElementById("cmdChangeUI").addEventListener("click", doClickChangeUI, false);
        },

        unload: function () {
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });

    function doClickHome() {
        window.location('/default.html');
    };

    function doClickTextBased() {
        window.location('/text-based.html');
    };

    function doClickGraphic() {
        window.location('/graphic.html');
    };

    function doClickChangeUI() {
        if (style == "dark") {
            swapStyleSheet("ui-dark.css", "ui-light.css");
            style = "light";
        } else {
            swapStyleSheet("ui-light.css", "ui-dark.css");
            style = "dark";
        }
    }

    // Replaces the base style sheet
    function swapStyleSheet(oldStyleSheet, newStyleSheet) {
        var linkTag = document.querySelector("link[href$='" + oldStyleSheet + "']");
        var index = linkTag.href.lastIndexOf("/");
        var baseURL = linkTag.href.slice(0, index + 1);
        linkTag.href = baseURL += newStyleSheet;
    }

})();
